﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace quanlynhasach
{
    public partial class frmNhacungcap : Form
    {
        Connection db = new Connection();
        public frmNhacungcap()
        {
            InitializeComponent();
        }

        private void frmNhacungcap_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.DataSource = db.getDS("Select * from NhaCungCap where MaNhaCungCap <> 'N/A'");
        }
        private void Enable(bool a)
        {
            dataGridView1.Enabled = !a;
            them.Enabled = !a;
            sua.Enabled = !a;
            xoa.Enabled = !a;
            luu.Enabled = a;
            huy.Enabled = a;
            ma.Enabled = a;
            ten.Enabled = a;
            DienThoai1.Enabled = a;
            DienThoai2.Enabled = a;
            diachi.Enabled = a;
            mathue.Enabled = a;
            email.Enabled = a;
            fax.Enabled = a;
        }

        public bool XoaNCC(string MaNhaCungCap)
        {
            string[] param = { "@MaNhaCungCap" };
            object[] value = { MaNhaCungCap };
            string query = "Delete from NhaCungCap where MaNhaCungCap=@MaNhaCungCap";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        public bool SuaNCC(string ma, string ten, string diachi, string DienThoai1, string DienThoai2, string mathue, string email, string fax)
        {
            string[] param = { "@MaNhaCungCap", "@TenNCC", "@DiaChi", "@DienThoai1", "@DienThoai2", "@MaSoThue", "@Email", "@Fax" };
            object[] value = { ma, ten, diachi, DienThoai1, DienThoai2, mathue, email, fax };
            string query = "Update NhaCungCap set TenNCC=@TenNCC, DiaChi=@DiaChi, DienThoai1=@DienThoai1, DienThoai2=@DienThoai2, MaSoThue=@MaSoThue, Email=@Email, Fax=@Fax where MaNhaCungCap=@MaNhaCungCap";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        public bool ThemNCC(string ma, string ten, string diachi, string DienThoai1, string DienThoai2, string mathue, string email, string fax)
        {
            string[] param = { "@MaNhaCungCap", "@TenNCC", "@DiaChi", "@DienThoai1", "@DienThoai2", "@MaSoThue", "@Email", "@Fax" };
            object[] value = { ma, ten, diachi, DienThoai1, DienThoai2, mathue, email, fax };
            string query = "Insert into NhaCungCap (MaNhaCungCap, TenNCC, DiaChi, DienThoai1, DienThoai2, MaSoThue, Email, Fax) values (@MaNhaCungCap, @TenNCC, @DiaChi, @DienThoai1, @DienThoai2, @MaSoThue, @Email, @Fax)";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        private void them_Click(object sender, EventArgs e)
        {
            Enable(true);
            ma.Text = "";
            ten.Text = "";
            diachi.Text = "";
            DienThoai1.Text = "";
            DienThoai2.Text = "";
            mathue.Text = "";
            email.Text = "";
            fax.Text = "";
        }

        private void sua_Click(object sender, EventArgs e)
        {
            Enable(true);
        }

        private void xoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc là nha cung cap " + ma.Text + " này không ?. Nếu xóa, tất cả dữ liệu liên quan sẽ bị xóa theo.", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                if (XoaNCC(ma.Text))
                    MessageBox.Show("Xóa thành công !");
            dataGridView1.DataSource = db.getDS("Select * from NhaCungCap where MaNhaCungCap <> 'N/A'");
        }

        private void luu_Click(object sender, EventArgs e)
        {
            if (db.getDS("Select * from NhaCungCap where MaNhaCungCap = '" + ma.Text + "'").Rows.Count > 0)
                if (SuaNCC(ma.Text, ten.Text, diachi.Text, DienThoai1.Text, DienThoai2.Text, mathue.Text, email.Text, fax.Text))
                    MessageBox.Show("Cập nhật thành công");
                else
                    MessageBox.Show("Xem lại !");
            else
                if (ThemNCC(ma.Text, ten.Text, diachi.Text, DienThoai1.Text, DienThoai2.Text, mathue.Text, email.Text, fax.Text))
                    MessageBox.Show("Thêm thành công !");
                else
                    MessageBox.Show("Xem lại !");
            dataGridView1.DataSource = db.getDS("Select * from NhaCungCap where MaNhaCungCap <> 'N/A'");
            Enable(false);
        }

        private void huy_Click(object sender, EventArgs e)
        {
            Enable(false);
            dataGridView1.DataSource = db.getDS("Select * from NhaCungCap where MaNhaCungCap <> 'N/A'");
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int Rs = e.RowIndex;
            ma.Text = dataGridView1.Rows[Rs].Cells["MaNhaCungCap"].Value.ToString();
            ten.Text = dataGridView1.Rows[Rs].Cells["TenNCC"].Value.ToString();
            diachi.Text = dataGridView1.Rows[Rs].Cells["DiaChi"].Value.ToString();
            DienThoai1.Text = dataGridView1.Rows[Rs].Cells["DienThoai1"].Value.ToString();
            DienThoai2.Text = dataGridView1.Rows[Rs].Cells["DienThoai2"].Value.ToString();
            mathue.Text = dataGridView1.Rows[Rs].Cells["MaSoThue"].Value.ToString();
            email.Text = dataGridView1.Rows[Rs].Cells["Email"].Value.ToString();
            fax.Text = dataGridView1.Rows[Rs].Cells["Fax"].Value.ToString();
        }
    }
}
